package com.angus.modules.content.controller.manage;

import com.angus.common.core.web.controller.BaseController;
import com.angus.common.core.web.domain.AjaxResult;
import com.angus.common.security.utils.SecurityUtils;
import com.angus.modules.content.domain.Course;
import com.angus.modules.content.dto.AddCourseDto;
import com.angus.modules.content.dto.ClearCourseDto;
import com.angus.modules.content.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author liyanan
 */
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {

    @Autowired
    private ICourseService courseService;

    @GetMapping("/courseList")
    public AjaxResult getCourseList(){
        List<Course> list = courseService.getCourseList();
        return AjaxResult.success(list);
    }

    @GetMapping("/upCourseList")
    public AjaxResult getUpCourseList(@RequestParam String type){
        List<Course> list = courseService.getUpCourseList(type);
        return AjaxResult.success(list);
    }


    @GetMapping("/getCourseByType")
    public AjaxResult getCourseByType(@RequestParam(required = false, defaultValue = "") String type){
        List<Course> list = courseService.getCourseByType(type);
        return AjaxResult.success(list);
    }

    @DeleteMapping("/clearCourse")
    public AjaxResult clearCourse(@Validated @RequestBody ClearCourseDto params){
        try {
            courseService.clearCourse(params.getCourseCode(), params.getType());
            return AjaxResult.success("清除课程相关数据成功");
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @DeleteMapping("/clearQuestion")
    public AjaxResult clearQuestion(@Validated @RequestBody ClearCourseDto params){
        try {
            courseService.clearQuestion(params.getCourseCode(), params.getType());
            return AjaxResult.success("清除课程相关练习题数据成功");
        }catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody AddCourseDto dto, BindingResult result){
        if (Objects.nonNull(toBindResult(result))) {
            return AjaxResult.error(toBindResult(result));
        }
        Course course = courseService.addCourse(dto);
        return AjaxResult.success(course);
    }

    @DeleteMapping("/remove")
    public AjaxResult remove(@RequestBody List<Long> ids){
        if (ids == null || ids.isEmpty()) {
            return AjaxResult.warn("ID列表不能为空");
        }
        List<Long> existingIds = courseService.getByIds(ids);
        List<Long> nonExistingIds = ids.stream().filter(id -> !existingIds.contains(id)).collect(Collectors.toList());
        if (!nonExistingIds.isEmpty()) {
            return AjaxResult.warn("部分ID不存在: " + nonExistingIds);
        }
        if(courseService.deleteByIds(ids)){
            return AjaxResult.success("删除成功，共计："+ ids.size()+" 条");
        }
        return AjaxResult.error("删除失败");
    }


    @GetMapping("/detail/{id}")
    public AjaxResult getCourseDetail(@PathVariable("id") Long id){

        Course course = courseService.getById(id);
        return AjaxResult.success(course);
    }

    @PutMapping("/modifyCourse")
    public AjaxResult modifyCourse(@RequestBody Course course){

        boolean result = courseService.modifyCourse(course);
        return toAjax(result);
    }

    @PutMapping("/updateStatus/{id}/{value}")
    public AjaxResult updateStatus(@PathVariable("id") Long id,
                                   @PathVariable("value")
                                   @Min(value = 0, message = "状态值必须为0或1")
                                   @Max(value = 1, message = "状态值必须为0或1")
                                   @NotNull(message = "状态值不能为空") Integer value){

        boolean result = courseService.updateStatus(id, value);
        return toAjax(result);
    }
}
